import time

from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from base.base_page import BasePage

capabilities ={
  "platformName": "Android",
  "appium:platformVersion": "12.0",
  "appium:deviceName": "emulator-5554",
  "appium:appPackage": "com.tencent.wework",
  "appium:appActivity": "com.tencent.wework.launch.LaunchSplashActivity",
  "appium:noReset": True,
  "appium:dontStopAppOnReset": False,
  "appium:autoGrantPermissions": False,
  "appium:automationName": "UIAutomator2",
  "appium:adbExecTimeout": "80000",
  "forceAppLaunch": True
}
appium_server_url = 'http://localhost:4723'
class Wework(BasePage):
    def setup_class(self):
        self.driver = webdriver.Remote(appium_server_url, options=UiAutomator2Options().load_capabilities(capabilities))
        self.wait = WebDriverWait(self.driver,30)

    def find_ele(self,locator):
        el = self.wait.until(expected_conditions.element_to_be_clickable(locator))
        return el

    def find_eles(self,locator):
        self.find_ele(locator)
        elements = self.driver.find_elements(locator)
        return elements

    def find_ele_when_scroll(self,by,value):
        try:
            return self.driver.find_element(by=by,value=value)
        except Exception :
            self.driver.swipe(100,100,200,200,duration= 1000)
            self.find_ele_when_scroll(by,value)

    def tearDown_class(self):
        if self.driver:
            self.driver.quit()
    def test_demo_scroll(self):
        # 滑动操作--推荐使用该方法
        # time.sleep(10)
        # 查找通讯录
        el = self.wait.until(expected_conditions.element_to_be_clickable(
            (AppiumBy.XPATH, '//android.widget.TextView[@resource-id="com.tencent.wework:id/h1o" and @text="通讯录"]')))

        el.click()
        # time.sleep(2)
        # 添加成员
        # el = self.find_ele_when_scroll(AppiumBy.XPATH,
        #                                '//android.widget.TextView[@resource-id="com.tencent.wework:id/mid1Txt" and @text="添加成员"]')
        time.sleep(2)
        self.driver.swipe(300,300,100,100,duration=500)
        while 1:
            time.sleep(5)
            # 从屏幕四分之三的位置滑动到四分之一的位置
            self.driver.swipe(self.driver.get_window_size()['width'] / 2, self.driver.get_window_size()['height'] * 3 / 4, self.driver.get_window_size()['width'] / 2, self.driver.get_window_size()['height'] * 1 / 4, 2000) # 通过坐标进行滑动操作滑动时间为3秒
            time.sleep(5)
            self.driver.swipe(self.driver.get_window_size()['width'] / 2, self.driver.get_window_size()['height'] * 1 / 4,self.driver.get_window_size()['width'] / 2, self.driver.get_window_size()['height'] * 3 / 4,2000 ) # 通过坐标进行滑动操作滑动时间为3秒




